#-----------------------------------#
# Replication of:
# Seeking Opportunity in the Knowledge Economy: Moving Places, Moving Politics?
# Valentina Consiglio, Thomas Kurer

# File: 6_models_rob-het

# Note: This file takes the following steps:
# (1) Runs models included as robustness checks
# (2) Runs models included as heterogeneity analyses
# The data is loaded and prepared in 5_models_main_NUTS3; ensure the script was run before this file. 
#-----------------------------------#



#-------------------------------------------------------------------------------# 
#### Preparation ####
#-------------------------------------------------------------------------------# 

## Texreg input 
vec_gof_names <- c("Num. obs." = "N",
                   "Num. groups: pid" = "N individuals",
                   "Num. groups: syear" = "N years")



#-------------------------------------------------------------------------------# 
#### 1 | Robustness ####
#-------------------------------------------------------------------------------# 


#-------------------------------------------------------------------------------# 
##### 1.1 | Employment status and income as controls #####
#-------------------------------------------------------------------------------# 

# EMPLOYMENT STATUS #

## Political integration and orientation ##

fe_naive_polint_polor2_af_emplst_aggr <-  list(
  "Volunteering (Yes/No)" = feols(ft_volunt_d ~ opportunity_pc01_wn + as.factor(af_emplst_aggr) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                  | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Local political activism (Yes/No)" = feols(ft_localpol_d ~ opportunity_pc01_wn + as.factor(af_emplst_aggr) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                              | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Voting in federal election (Yes/No)" = feols(votefedel_lead ~ opportunity_pc01_wn + as.factor(af_emplst_aggr) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                                | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009),
  
  "Party leaning (Yes/No)" = feols(partyleand2 ~ opportunity_pc01_wn + as.factor(af_emplst_aggr) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                   | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Party leaning intensity (0-5)" = feols(partyleanint ~ opportunity_pc01_wn + as.factor(af_emplst_aggr) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                          | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Political orientation (0 L – 10 R)" = feols(polor ~ opportunity_pc01_wn + as.factor(af_emplst_aggr) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                               | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009),
  
  "Centre left party ID" = feols(centre_left2 ~ opportunity_pc01_wn + as.factor(af_emplst_aggr) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                 | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Centre right party ID" = feols(centre_right2 ~ opportunity_pc01_wn + as.factor(af_emplst_aggr) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) |
                                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df)
)


texreg(fe_naive_polint_polor2_af_emplst_aggr)


texreg(fe_naive_polint_polor2_af_emplst_aggr, digits = 3,
       label = "tab:fe_naive_polint_polor2_af_emplst_aggr",
       caption = "Opportunity moves and political integration and orientation  (including employment status as control)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                             
       custom.note = paste("%stars. All models include age group, education group, household type, and employment status as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2009/10-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D1.tex"))


#--------------------------------------------------------------------------------------------#
## Party identification ##

# - alternative coding where all those with 0 party identification are in the reference category of the dummies

fe_naive_partid2_af_emplst_aggr <-  list(
  "SPD " = feols(spd_co2 ~ opportunity_pc01_wn + as.factor(af_emplst_aggr) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "CDU/CSU " = feols(cducsu_co2 ~ opportunity_pc01_wn + as.factor(af_emplst_aggr) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                       pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Gruene " = feols(greens_co2 ~ opportunity_pc01_wn + as.factor(af_emplst_aggr) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                      pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "FDP " = feols(fdp_co2 ~ opportunity_pc01_wn + as.factor(af_emplst_aggr) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Linke" = feols(linke_co2 ~ opportunity_pc01_wn + as.factor(af_emplst_aggr) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "AfD " = feols(afd_co2 ~ opportunity_pc01_wn + as.factor(af_emplst_aggr) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2014)
)

texreg(fe_naive_partid2_af_emplst_aggr)


texreg(fe_naive_partid2_af_emplst_aggr, digits = 3,
       label = "tab:fe_main_partid2_af_emplst_aggr",
       caption = "Opportunity moves and party identification (including employment status as control)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                            
       custom.note = paste("%stars. Party leanings include coalitions in the political direction of the respective party (i.e., including left coalitions for SPD, Gruene, and Linke; right coalitions for CDU/CSU, FDP, and AfD). All models include age group, education group, household type, and employment status as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2010-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D2.tex"))


# INCOME #

## Political integration and orientation ##

fe_naive_polint_polor2_income <-  list(
  "Volunteering (Yes/No)" = feols(ft_volunt_d ~ opportunity_pc01_wn + income + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                  | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Local political activism (Yes/No)" = feols(ft_localpol_d ~ opportunity_pc01_wn + income + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                              | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Voting in federal election (Yes/No)" = feols(votefedel_lead ~ opportunity_pc01_wn + income + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                                | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009),
  
  "Party leaning (Yes/No)" = feols(partyleand2 ~ opportunity_pc01_wn + income + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                   | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Party leaning intensity (0-5)" = feols(partyleanint ~ opportunity_pc01_wn + income + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                          | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Political orientation (0 L – 10 R)" = feols(polor ~ opportunity_pc01_wn + income + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                               | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009),
  
  "Centre left party ID" = feols(centre_left2 ~ opportunity_pc01_wn + income + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                 | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Centre right party ID" = feols(centre_right2 ~ opportunity_pc01_wn + income + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) |
                                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df)
)


texreg(fe_naive_polint_polor2_income)


texreg(fe_naive_polint_polor2_income, digits = 3,
       label = "tab:fe_naive_polint_polor2_income",
       caption = "Opportunity moves and political integration and orientation  (including income as control)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                             
       custom.note = paste("%stars. All models include age group, education group, household type, and household equivalized disposable income as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2009/10-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D3.tex"))


#--------------------------------------------------------------------------------------------#
## Party identification ##

# - alternative coding where all those with 0 party identification are in the reference category of the dummies

fe_naive_partid2_income <-  list(
  "SPD " = feols(spd_co2 ~ opportunity_pc01_wn + income + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "CDU/CSU " = feols(cducsu_co2 ~ opportunity_pc01_wn + income + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                       pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Gruene " = feols(greens_co2 ~ opportunity_pc01_wn + income + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                      pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "FDP " = feols(fdp_co2 ~ opportunity_pc01_wn + income + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Linke" = feols(linke_co2 ~ opportunity_pc01_wn + income + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "AfD " = feols(afd_co2 ~ opportunity_pc01_wn + income + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2014)
)

texreg(fe_naive_partid2_income)


texreg(fe_naive_partid2_income, digits = 3,
       label = "tab:fe_naive_partid2_income",
       caption = "Opportunity moves and party identification (including income as control)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                            
       custom.note = paste("%stars. Party leanings include coalitions in the political direction of the respective party (i.e., including left coalitions for SPD, Gruene, and Linke; right coalitions for CDU/CSU, FDP, and AfD). All models include age group, education group, household type, and household equivalized disposable income as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2010-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D4.tex"))



#-------------------------------------------------------------------------------# 
##### 1.2 | Exclude multiple movers #####
#-------------------------------------------------------------------------------# 

fe_main_polint_polor2_womm <-  list(
  "Volunteering (Yes/No)" = feols(ft_volunt_d ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                  | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$mm_ub2 != 1, ]),
  
  "Local political activism (Yes/No)" = feols(ft_localpol_d ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                              | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$mm_ub2 != 1, ]),
  
  "Voting in federal election (Yes/No)" = feols(votefedel_lead ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                                | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009[df_2009$mm_ub2 != 1, ]),
  
  "Party leaning (Yes/No)" = feols(partyleand2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                   | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$mm_ub2 != 1, ]),
  
  "Party leaning intensity (0-5)" = feols(partyleanint ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                          | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$mm_ub2 != 1, ]),
  
  "Political orientation (0 L – 10 R)" = feols(polor ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                               | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009[df_2009$mm_ub2 != 1, ]),
  
  "Centre left party ID" = feols(centre_left2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                 | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$mm_ub2 != 1, ]),
  
  "Centre right party ID" = feols(centre_right2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) |
                                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$mm_ub2 != 1, ])
)


texreg(fe_main_polint_polor2_womm)


texreg(fe_main_polint_polor2_womm, digits = 3,
       label = "tab:fe_main_polint_polor2_womm",
       caption = "Opportunity moves and political integration and orientation (without multiple movers)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                             
       custom.note = paste("%stars. All models exclude multiple movers from the sample and include age group, education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2010-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D5.tex"))


fe_main_partid2_womm <-  list(
  "SPD" = feols(spd_co2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                  pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$mm_ub2 != 1, ]),
  "CDU/CSU" = feols(cducsu_co2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                      pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$mm_ub2 != 1, ]),
  "Gruene" = feols(greens_co2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                     pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$mm_ub2 != 1, ]),
  "FDP" = feols(fdp_co2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                  pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$mm_ub2 != 1, ]),
  "Linke" = feols(linke_co2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$mm_ub2 != 1, ]),
  "AfD" = feols(afd_co2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                  pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2014[df_2014$mm_ub2 != 1, ])
)

texreg(fe_main_partid2_womm)


texreg(fe_main_partid2_womm, digits = 3,
       label = "tab:fe_main_partid2_womm",
       caption = "Opportunity moves and party identification (without multiple movers)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                            
       custom.note = paste("%stars. All models exclude multiple movers from the sample and include age group, education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2010-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D6.tex"))


#-------------------------------------------------------------------------------# 
##### 1.3 | Education restriction (post HE waves) #####
#-------------------------------------------------------------------------------# 


fe_main_polint_polor2_educrestr <-  list(
  "Volunteering (Yes/No)" = feols(ft_volunt_d ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                  | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$educ_filter == 1, ]),
  
  "Local political activism (Yes/No)" = feols(ft_localpol_d ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                              | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$educ_filter == 1, ]),
  
  "Voting in federal election (Yes/No)" = feols(votefedel_lead ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                                | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009[df_2009$educ_filter == 1, ]),
  
  "Party leaning (Yes/No)" = feols(partyleand2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                   | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$educ_filter == 1, ]),
  
  "Party leaning intensity (0-5)" = feols(partyleanint ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                          | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$educ_filter == 1, ]),
  
  "Political orientation (0 L – 10 R)" = feols(polor ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                               | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009[df_2009$educ_filter == 1, ]),
  
  "Centre left party ID" = feols(centre_left2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                 | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$educ_filter == 1, ]),
  
  "Centre right party ID" = feols(centre_right2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) |
                                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$educ_filter == 1, ])
)


texreg(fe_main_polint_polor2_educrestr)


texreg(fe_main_polint_polor2_educrestr, digits = 3,
       label = "tab:fe_main_polint_polor2_educrestr",
       caption = "Opportunity moves and political integration and orientation (education restriction)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                             
       custom.note = paste("%stars. All models are restricted to post-university years in the case of higher educated and include age group, education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2009/10-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D7.tex"))



fe_main_partid2_educrestr <-  list(
  "SPD" = feols(spd_co2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                  pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$educ_filter == 1, ]),
  
  "CDU/CSU" = feols(cducsu_co2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                      pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$educ_filter == 1, ]),
  
  "Gruene" = feols(greens_co2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                     pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$educ_filter == 1, ]),
  
  "FDP" = feols(fdp_co2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                  pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$educ_filter == 1, ]),
  
  "Linke" = feols(linke_co2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df[df$educ_filter == 1, ]),
  
  "AfD" = feols(afd_co2 ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                  pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2014[df_2014$educ_filter == 1, ])
)

texreg(fe_main_partid2_educrestr)


texreg(fe_main_partid2_educrestr, digits = 3,
       label = "tab:fe_main_partid2_educrestr",
       caption = "Opportunity moves and party identification  (education restriction)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                            
       custom.note = paste("%stars. All models are restricted to post-university years in the case of higher educated and include age group, education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2010-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D8.tex"))




#-------------------------------------------------------------------------------# 
##### 1.4 | Regional fixed effects (NSWE) #####
#-------------------------------------------------------------------------------# 

df_sample <- df %>%
  filter(!is.na(spd_co2),
         !is.na(opportunity_pc01_wn),
         !is.na(nuts1),
         !is.na(age_grp3),
         !is.na(educ_h),
         !is.na(hhtype),
         !is.na(krs)
         )

table(df$moved_krs2, df$region)

table(df_sample$moved_krs2, df_sample$region)


# Rows west, columns west_lag1
nswe_destination <- as.data.frame(table(df_sample$moved_krs2, df_sample$region))

# Rename columns
colnames(nswe_destination) <- c("moved", "destination", "count") 

# Calculate relative frequencies (percentage)
nswe_destination <- nswe_destination %>%
  filter(moved == 1)
total <- sum(nswe_destination$count)

nswe_destination <- nswe_destination %>%
  mutate(share = round((count / total)*100, 2),
         destination_lbl = case_when(
           destination == 1 ~ "North",
           destination == 2 ~ "South",
           destination == 3 ~ "West",
           destination == 4 ~ "East")
  )
nswe_destination

## North / South / West / East (NSWE) ##

#-------------------------------------------------------------------------------# 
## Political integration and orientation ##
#-------------------------------------------------------------------------------# 


fe_main_polint_polor2_nswe_ctrl <-  list(
  "Volunteering (Yes/No)" = feols(ft_volunt_d ~ opportunity_pc01_wn + as.factor(region) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                  | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Local political activism (Yes/No)" = feols(ft_localpol_d ~ opportunity_pc01_wn + as.factor(region) +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                              | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Voting in federal election (Yes/No)" = feols(votefedel_lead ~ opportunity_pc01_wn + as.factor(region) +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                                | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009),
  
  "Party leaning (Yes/No)" = feols(partyleand2 ~ opportunity_pc01_wn + as.factor(region) +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                   | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Party leaning intensity (0-5)" = feols(partyleanint ~ opportunity_pc01_wn + as.factor(region) +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                          | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Political orientation (0 L – 10 R)" = feols(polor ~ opportunity_pc01_wn + as.factor(region) +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                               | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009),
  
  "Centre left party ID" = feols(centre_left2 ~ opportunity_pc01_wn + as.factor(region) +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                 | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Centre right party ID" = feols(centre_right2 ~ opportunity_pc01_wn + as.factor(region) +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) |
                                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df)
)


texreg(fe_main_polint_polor2_nswe_ctrl)


texreg(fe_main_polint_polor2_nswe_ctrl, digits = 3,
       label = "tab:fe_main_polint_polor2_nswe_ctrl",
       caption = "Opportunity moves and political integration and orientation (including NSWE control)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                             
       custom.note = paste("%stars. All models include age group, education group, household type, and categorical regional indicator (north, south, west, east) as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2009/10-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D25.tex"))


#--------------------------------------------------------------------------------------------#

#-------------------------------------------------------------------------------# 
## Party identification ##
#-------------------------------------------------------------------------------# 

# - alternative coding where all those with 0 party identification are in the reference category of the dummies

fe_main_partid2_nswe_ctrl <-  list(
  "SPD " = feols(spd_co2 ~ opportunity_pc01_wn  + as.factor(region) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "CDU/CSU " = feols(cducsu_co2 ~ opportunity_pc01_wn + as.factor(region) +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                       pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Gruene " = feols(greens_co2 ~ opportunity_pc01_wn + as.factor(region) +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                      pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "FDP " = feols(fdp_co2 ~ opportunity_pc01_wn + as.factor(region) +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Linke" = feols(linke_co2 ~ opportunity_pc01_wn + as.factor(region) +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "AfD " = feols(afd_co2 ~ opportunity_pc01_wn + as.factor(region) +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2014)
)

texreg(fe_main_partid2_nswe_ctrl)


texreg(fe_main_partid2_nswe_ctrl, digits = 3,
       label = "tab:fe_main_partid2_nswe_ctrl",
       caption = "Opportunity moves and party identification (including NSWE control)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                            
       custom.note = paste("%stars. Party leanings include coalitions in the political direction of the respective party (i.e., including left coalitions for SPD, Gruene, and Linke; right coalitions for CDU/CSU, FDP, and AfD). All models include age group, education group, household type, and and categorical regional indicator (north, south, west, east) as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2010-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D26.tex"))

#--------------------------------------------------------------------------------------------#


#-------------------------------------------------------------------------------# 
#### 2 | Heterogeneity ####
#-------------------------------------------------------------------------------# 

#-------------------------------------------------------------------------------# 
##### 2.1 | Index quintiles #####
#-------------------------------------------------------------------------------# 


list_oiqtl_wn <-  list('as.factor(oiqtl_wn)2' = 'Opportunity Index Q2',
                       'as.factor(oiqtl_wn)3' = 'Opportunity Index Q3',
                       'as.factor(oiqtl_wn)4' = 'Opportunity Index Q4',
                       'as.factor(oiqtl_wn)5' = 'Opportunity Index Q5'
)

## Opportunity moves and political integration and orientation ##

fe_main_polint_polor2_oiqtl <-  list(
  
  "Volunteering (Yes/No)" = feols(ft_volunt_d ~ as.factor(oiqtl_wn) + as.factor(age_grp3) + as.factor(educ) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Local political activism (Yes/No)" = feols(ft_localpol_d ~ as.factor(oiqtl_wn) + as.factor(age_grp3) + as.factor(educ) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Party leaning (Yes/No)" = feols(partyleand2 ~ as.factor(oiqtl_wn) + as.factor(age_grp3) + as.factor(educ) + as.factor(hhtype)| pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Party leaning intensity (0-5)" = feols(partyleanint ~ as.factor(oiqtl_wn) + as.factor(age_grp3) + as.factor(educ) + as.factor(hhtype)| pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Political orientation (0 L – 10 R)" = feols(polor ~ as.factor(oiqtl_wn) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009),
  "Centre left party ID" = feols(centre_left2 ~ as.factor(oiqtl_wn) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Centre right party ID" = feols(centre_right2 ~ as.factor(oiqtl_wn) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df)
)


texreg(fe_main_polint_polor2_oiqtl)

texreg(fe_main_polint_polor2_oiqtl, digits = 3,
       label = "tab:fe_oiqtl_polor2",
       caption = "Opportunity moves by quintiles and political integration and orientation",
       custom.coef.map = list_oiqtl_wn,
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                           
       custom.note = paste("%stars. All models include age group, education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2009/10-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D9.tex"))

## Party identification ##

# With alternative coding where all those with 0 party identification are in the reference category of the dummies

fe_main_partid2_oiqtl <-  list(
  "SPD " = feols(spd_co2 ~ as.factor(oiqtl_wn) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "CDU/CSU " = feols(cducsu_co2 ~ as.factor(oiqtl_wn) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Gruene " = feols(greens_co2 ~ as.factor(oiqtl_wn) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "FDP " = feols(fdp_co2 ~ as.factor(oiqtl_wn) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Linke" = feols(linke_co2 ~ as.factor(oiqtl_wn) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "AfD " = feols(afd_co2 ~ as.factor(oiqtl_wn) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2014)
)

texreg(fe_main_partid2_oiqtl)


texreg(fe_main_partid2_oiqtl, digits = 3,
       label = "tab:fe_main_partid2_oiqtl",
       caption = "Opportunity moves by quintiles and party identification",
       custom.coef.map = list_oiqtl_wn,
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES"), 
                              "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                             
       custom.note = paste("%stars. All models include age group, education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2010-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D10.tex"))




#-------------------------------------------------------------------------------# 
##### 2.2 | Moving distance #####
#-------------------------------------------------------------------------------# 

## Opportunity moves and political integration and orientation ##

# Categorical Kreis-level move distance variable (4 categories)
fe_main_polint_polor_distance_cat_krs <-  list(
  "Volunteering (Yes/No)" = feols(ft_volunt_d ~ opportunity_pc01_wn*as.factor(distance_cat_krs) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Local political activism (Yes/No)" = feols(ft_localpol_d ~ opportunity_pc01_wn*as.factor(distance_cat_krs) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Voting in federal election (Yes/No)" = feols(votefedel_lead ~ opportunity_pc01_wn*as.factor(distance_cat_krs) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009),
  
  "Party leaning (Yes/No)" = feols(partyleand2 ~ opportunity_pc01_wn*as.factor(distance_cat_krs) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)| pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Party leaning intensity (0-5)" = feols(partyleanint ~ opportunity_pc01_wn*as.factor(distance_cat_krs) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)| pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Political orientation (0 L – 10 R)" = feols(polor ~ opportunity_pc01_wn*as.factor(distance_cat_krs) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009),
  "Centre left party ID" = feols(centre_left2 ~ opportunity_pc01_wn*as.factor(distance_cat_krs) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Centre right party ID" = feols(centre_right2 ~ opportunity_pc01_wn*as.factor(distance_cat_krs) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df)
)


texreg(fe_main_polint_polor_distance_cat_krs)


texreg(fe_main_polint_polor_distance_cat_krs, digits = 3,
       label = "tab:fe_main_polint_polor_distance_cat",
       caption = "Opportunity moves and political integration and orientation by moving distance",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index',
                              'as.factor(distance_cat_krs)2' = 'Distance 51–200 km',
                              'as.factor(distance_cat_krs)3' = 'Distance 201–400 km',
                              'as.factor(distance_cat_krs)4' = 'Distance >400 km',
                              'opportunity_pc01_wn:as.factor(distance_cat_krs)2' = 'Opportunity X Distance 51-200 km',
                              'opportunity_pc01_wn:as.factor(distance_cat_krs)3' = 'Opportunity X Distance 201-400 km',
                              'opportunity_pc01_wn:as.factor(distance_cat_krs)4' = 'Opportunity X Distance >400 km'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                             
       custom.note = paste("%stars. Reference distance category: 0-50 km. All models include age group, education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2009/10-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D11.tex"))


#-------------------------------------------------------------------------------# 

## Party identification ##


# Distance 4 categories
fe_main_partid2_distance_cat_krs <-  list(
  "SPD " = feols(spd_co2 ~ opportunity_pc01_wn*as.factor(distance_cat_krs) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "CDU/CSU " = feols(cducsu_co2 ~ opportunity_pc01_wn*as.factor(distance_cat_krs) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Gruene " = feols(greens_co2 ~ opportunity_pc01_wn*as.factor(distance_cat_krs) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "FDP " = feols(fdp_co2 ~ opportunity_pc01_wn*as.factor(distance_cat_krs) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Linke" = feols(linke_co2 ~ opportunity_pc01_wn*as.factor(distance_cat_krs) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "AfD " = feols(afd_co2 ~ opportunity_pc01_wn*as.factor(distance_cat_krs) + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2014)
)

texreg(fe_main_partid2_distance_cat_krs)


texreg(fe_main_partid2_distance_cat_krs, digits = 3,
       label = "tab:fe_main_partid2_distance_cat_krs",
       caption = "Opportunity moves and party identification by moving distance",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index',
                              'as.factor(distance_cat_krs)2' = 'Distance 51–200 km',
                              'as.factor(distance_cat_krs)3' = 'Distance 201–400 km',
                              'as.factor(distance_cat_krs)4' = 'Distance >400 km',
                              'opportunity_pc01_wn:as.factor(distance_cat_krs)2' = 'Opportunity X Distance 51-200 km',
                              'opportunity_pc01_wn:as.factor(distance_cat_krs)3' = 'Opportunity X Distance 201-400 km',
                              'opportunity_pc01_wn:as.factor(distance_cat_krs)4' = 'Opportunity X Distance >400 km'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                            
       custom.note = paste("%stars. Reference distance category: 0-50 km. All models include age group, education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2010-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D12.tex"))


#-------------------------------------------------------------------------------# 

#-------------------------------------------------------------------------------# 
##### 2.3 | Age groups #####
#-------------------------------------------------------------------------------# 
## Interactions ##
vec_coef_map <- list('opportunity_pc01_wn' = 'Opportunity Index',
                     'as.factor(age_grp3)2' = 'Age 30-49',
                     'as.factor(age_grp3)3' = 'Age 50-70',
                     'opportunity_pc01_wn:as.factor(age_grp3)2' = 'OI X Age 30-49',
                     'opportunity_pc01_wn:as.factor(age_grp3)3' = 'OI X Age 50-70'
)

## Opportunity moves and political integration and orientation ##

fe_main_polint_polor2_age <-  list(
  "Volunteering (Yes/No)" = feols(ft_volunt_d ~ opportunity_pc01_wn*as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Local political activism (Yes/No)" = feols(ft_localpol_d ~ opportunity_pc01_wn*as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Voting (Yes/No)" = feols(votefedel_lead ~ opportunity_pc01_wn*as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009),
  
  "Party leaning (Yes/No)" = feols(partyleand2 ~ opportunity_pc01_wn*as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)| pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Party leaning intensity (0-5)" = feols(partyleanint ~ opportunity_pc01_wn*as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)| pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Political orientation (0 L – 10 R)" = feols(polor ~ opportunity_pc01_wn*as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009),
  "Centre left party ID" = feols(centre_left2 ~ opportunity_pc01_wn*as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Centre right party ID" = feols(centre_right2 ~ opportunity_pc01_wn*as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df)
)

texreg(fe_main_polint_polor2_age)

texreg(fe_main_polint_polor2_age, digits = 3,
       label = "tab:fe_main_polint_polor2_age",
       caption = "Opportunity moves and political integration and orientation by age group",
       custom.coef.map = vec_coef_map,
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                             
       custom.note = paste("%stars. Reference age group 18-29; All models include education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2009/10-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D13.tex"))


## Party identification ##


fe_main_partid2_age <-  list(
  "SPD " = feols(spd_co2 ~ opportunity_pc01_wn*as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "CDU/CSU " = feols(cducsu_co2 ~ opportunity_pc01_wn*as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Gruene " = feols(greens_co2 ~ opportunity_pc01_wn*as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "FDP " = feols(fdp_co2 ~ opportunity_pc01_wn*as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Linke" = feols(linke_co2 ~ opportunity_pc01_wn*as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "AfD " = feols(afd_co2 ~ opportunity_pc01_wn*as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2014)
)

texreg(fe_main_partid2_age)


texreg(fe_main_partid2_age, digits = 3,
       label = "tab:fe_main_partid2_age",
       caption = "Opportunity moves and party identification by age group",
       custom.coef.map = vec_coef_map,
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                            
       custom.note = paste("%stars. Reference age group 18-29; All models include education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2010-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D14.tex"))



#-------------------------------------------------------------------------------# 
##### 2.4 | East/West #####
#-------------------------------------------------------------------------------# 

# Descriptives on moving direction
# (A) West - West
# (B) East - East
# (C) West - East
# (D) East - West

# East/West variable from SOEP 

moves <- df %>%
  group_by(pid, syear) %>%
  select(pid, syear, moved_krs2, west, west_lag1) %>%
  filter(moved_krs2 == 1)

table(moves$west)

# Rows west, columns west_lag1
east_west_moves <- as.data.frame(table(moves$west_lag1, moves$west))

# Rename columns
colnames(east_west_moves) <- c("origin", "destination", "count") 

# Calculate relative frequencies (percentage)
total <- sum(east_west_moves$count)

east_west_moves <- east_west_moves %>%
  mutate(share = round((count / total)*100, 2),
         transition = case_when(
           origin == 1 & destination == 1 ~ "west-west",
           origin == 0 & destination == 0 ~ "east-east",
           origin == 1 & destination == 0 ~ "west-east",
           origin == 0 & destination == 1 ~ "east-west")
         )
east_west_moves

# Create new variable

df_2009 <- df_2009 %>%
  mutate(ew_trans = case_when(west_lag1 == 1 & west == 1 ~ 1, # west-west
                              west_lag1 == 0 & west == 0 ~ 2, # east-east
                              west_lag1 == 1 & west == 0 ~ 3, # west-east
                              west_lag1 == 0 & west == 1 ~ 4, # east-west
                              is.na(west_lag1) == TRUE & west == 1 ~ 1, # code first year as west / east to avoid losing in models with ctrl
                              is.na(west_lag1) == TRUE & west == 0 ~ 2
         
                                                   ),
         af_ew_trans =  as.factor(ew_trans)
         )


df <- df %>%
  mutate(ew_trans = case_when(west_lag1 == 1 & west == 1 ~ 1, # west-west
                              west_lag1 == 0 & west == 0 ~ 2, # east-east
                              west_lag1 == 1 & west == 0 ~ 3, # west-east
                              west_lag1 == 0 & west == 1 ~ 4, # east-west
                              is.na(west_lag1) == TRUE & west == 1 ~ 1, # code first year as west / east to avoid losing in models with ctrl
                              is.na(west_lag1) == TRUE & west == 0 ~ 2
  ),
  af_ew_trans =  as.factor(ew_trans)
  )

df_2014 <- df_2014 %>%
  mutate(ew_trans = case_when(west_lag1 == 1 & west == 1 ~ 1, # west-west
                              west_lag1 == 0 & west == 0 ~ 2, # east-east
                              west_lag1 == 1 & west == 0 ~ 3, # west-east
                              west_lag1 == 0 & west == 1 ~ 4, # east-west
                              is.na(west_lag1) == TRUE & west == 1 ~ 1, # code first year as west / east to avoid losing in models with ctrl
                              is.na(west_lag1) == TRUE & west == 0 ~ 2
                              ),
         af_ew_trans = as.factor(ew_trans)
  )

## Control ##

#-------------------------------------------------------------------------------# 
## Political integration and orientation ##
#-------------------------------------------------------------------------------# 


fe_main_polint_polor2_ew_ctrl <-  list(
  "Volunteering (Yes/No)" = feols(ft_volunt_d ~ opportunity_pc01_wn + af_ew_trans + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                  | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Local political activism (Yes/No)" = feols(ft_localpol_d ~ opportunity_pc01_wn + af_ew_trans +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                              | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Voting in federal election (Yes/No)" = feols(votefedel_lead ~ opportunity_pc01_wn + af_ew_trans +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                                | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009),
  
  "Party leaning (Yes/No)" = feols(partyleand2 ~ opportunity_pc01_wn + af_ew_trans +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                   | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Party leaning intensity (0-5)" = feols(partyleanint ~ opportunity_pc01_wn + af_ew_trans +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                          | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Political orientation (0 L – 10 R)" = feols(polor ~ opportunity_pc01_wn + af_ew_trans +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                               | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009),
  
  "Centre left party ID" = feols(centre_left2 ~ opportunity_pc01_wn + af_ew_trans +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                 | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  
  "Centre right party ID" = feols(centre_right2 ~ opportunity_pc01_wn + af_ew_trans +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) |
                                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df)
)


texreg(fe_main_polint_polor2_ew_ctrl)


texreg(fe_main_polint_polor2_ew_ctrl, digits = 3,
       label = "tab:fe_main_polint_polor2_ew_ctrl",
       caption = "Opportunity moves and political integration and orientation (including East-West-transition control)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                             
       custom.note = paste("%stars. All models include age group, education group, household type, and categorical four-level east/west relocation indicator as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2009/10-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D15.tex"))


#--------------------------------------------------------------------------------------------#

#-------------------------------------------------------------------------------# 
## Party identification ##
#-------------------------------------------------------------------------------# 

# - alternative coding where all those with 0 party identification are in the reference category of the dummies

fe_main_partid2_ew_ctrl <-  list(
  "SPD " = feols(spd_co2 ~ opportunity_pc01_wn  + af_ew_trans + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "CDU/CSU " = feols(cducsu_co2 ~ opportunity_pc01_wn + af_ew_trans +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                       pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Gruene " = feols(greens_co2 ~ opportunity_pc01_wn + af_ew_trans +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                      pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "FDP " = feols(fdp_co2 ~ opportunity_pc01_wn + af_ew_trans +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "Linke" = feols(linke_co2 ~ opportunity_pc01_wn + af_ew_trans +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df),
  "AfD " = feols(afd_co2 ~ opportunity_pc01_wn + af_ew_trans +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2014)
)

texreg(fe_main_partid2_ew_ctrl)


texreg(fe_main_partid2_ew_ctrl, digits = 3,
       label = "tab:fe_main_partid2_ew_ctrl",
       caption = "Opportunity moves and party identification (including East-West-transition control)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                            
       custom.note = paste("%stars. Party leanings include coalitions in the political direction of the respective party (i.e., including left coalitions for SPD, Gruene, and Linke; right coalitions for CDU/CSU, FDP, and AfD). All models include age group, education group, household type, and categorical four-level east/west relocation indicator as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2010-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D16.tex"))

#--------------------------------------------------------------------------------------------#



## Subset ##


# flag all observations that have a transition across east/west

df_2009 <- df_2009 %>%
  group_by(pid) %>%
  mutate(flag_ewt = any(ew_trans %in% c(3,4))) %>%
  ungroup

df <- df %>%
  group_by(pid) %>%
  mutate(flag_ewt = any(ew_trans %in% c(3,4))) %>%
  ungroup

df_2014 <- df_2014 %>%
  group_by(pid) %>%
  mutate(flag_ewt = any(ew_trans %in% c(3,4))) %>%
  ungroup

#--------------------------------------------------------------------------------------------#
#--------------------------------------------------------------------------------------------#
  
  ## West-West only moves! ##
  
  # flag all observations that have a transition across east/west
  
  df_2009 <- df_2009 %>%
  group_by(pid) %>%
  mutate(flag_west = any(ew_trans == 1) & flag_ewt == FALSE, # west-west (moved or stayed), and no cross transition
         flag_east = any(ew_trans == 2) & flag_ewt == FALSE,
         flag_west2 = flag_west == TRUE & flag_east == FALSE, # corrects inaccuracies from gaps
         flag_east2 = flag_east == TRUE & flag_west == FALSE) %>%
  ungroup()

table(df_2009$flag_east2, useNA = "always")
table(df_2009$flag_west2, useNA = "always")
table(df_2009$ew_trans, useNA = "always")
table(df_2009$flag_ewt, useNA = "always")

df <- df %>%
  group_by(pid) %>%
  mutate(flag_west = any(ew_trans == 1) & flag_ewt == FALSE, # west-west (moved or stayed), and no cross transition
         flag_east = any(ew_trans == 2) & flag_ewt == FALSE,
         flag_west2 = flag_west == TRUE & flag_east == FALSE, # corrects inaccuracies from gaps
         flag_east2 = flag_east == TRUE & flag_west == FALSE) %>%
  ungroup()

df_2014 <- df_2014 %>%
  group_by(pid) %>%
  mutate(flag_west = any(ew_trans == 1) & flag_ewt == FALSE, # west-west (moved or stayed), and no cross transition
         flag_east = any(ew_trans == 2) & flag_ewt == FALSE,
         flag_west2 = flag_west == TRUE & flag_east == FALSE, # corrects inaccuracies from gaps
         flag_east2 = flag_east == TRUE & flag_west == FALSE) %>%
  ungroup()

#-------------------------------------------------------------------------------# 
## Political integration and orientation ##
#-------------------------------------------------------------------------------# 

fe_main_polint_polor2_subset_ww <-  list(
  "Volunteering (Yes/No)" = feols(ft_volunt_d ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                  | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_west2)),
  
  "Local political activism (Yes/No)" = feols(ft_localpol_d ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                              | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_west2)),
  
  "Voting in federal election (Yes/No)" = feols(votefedel_lead ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                                | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009 %>% filter(flag_west2)),
  
  "Party leaning (Yes/No)" = feols(partyleand2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                   | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_west2)),
  
  "Party leaning intensity (0-5)" = feols(partyleanint ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                          | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_west2)),
  
  "Political orientation (0 L – 10 R)" = feols(polor ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                               | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009 %>% filter(flag_west2)),
  
  "Centre left party ID" = feols(centre_left2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                 | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_west2)),
  
  "Centre right party ID" = feols(centre_right2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) |
                                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_west2))
)


texreg(fe_main_polint_polor2_subset_ww)


texreg(fe_main_polint_polor2_subset_ww, digits = 3,
       label = "tab:fe_main_polint_polor2_subset_ww",
       caption = "Opportunity moves and political integration and orientation (west only)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                             
       custom.note = paste("%stars. All models are restricted to stayers and movers within west Germany and include age group, education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2009/10-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D17.tex"))


#--------------------------------------------------------------------------------------------#

#-------------------------------------------------------------------------------# 
## Party identification ##
#-------------------------------------------------------------------------------# 


# - alternative coding where all those with 0 party identification are in the reference category of the dummies

fe_main_partid2_subset_ww <-  list(
  "SPD " = feols(spd_co2 ~ opportunity_pc01_wn  + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_west2)),
  
  "CDU/CSU " = feols(cducsu_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                       pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_west2)),
  
  "Gruene " = feols(greens_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                      pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_west2)),
  
  "FDP " = feols(fdp_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_west2)),
  
  "Linke" = feols(linke_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_west2)),
  
  "AfD " = feols(afd_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2014 %>% filter(flag_west2))
)

texreg(fe_main_partid2_subset_ww)


texreg(fe_main_partid2_subset_ww, digits = 3,
       label = "tab:fe_main_partid2_subset_ww",
       caption = "Opportunity moves and party identification (west only)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                            
       custom.note = paste("%stars. Party leanings include coalitions in the political direction of the respective party (i.e., including left coalitions for SPD, Gruene, and Linke; right coalitions for CDU/CSU, FDP, and AfD). All models are restricted to stayers and movers within west Germany and include age group, education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2010-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D18.tex"))


#-------------------------------------------------------------------------------# 

#-------------------------------------------------------------------------------# 
## Political integration and orientation ##
#-------------------------------------------------------------------------------# 

fe_main_polint_polor2_subset_ee <-  list(
  "Volunteering (Yes/No)" = feols(ft_volunt_d ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                  | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_east2)),
  
  "Local political activism (Yes/No)" = feols(ft_localpol_d ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                              | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_east2)),
  
  "Voting in federal election (Yes/No)" = feols(votefedel_lead ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                                | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009 %>% filter(flag_east2)),
  
  "Party leaning (Yes/No)" = feols(partyleand2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                   | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_east2)),
  
  "Party leaning intensity (0-5)" = feols(partyleanint ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                          | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_east2)),
  
  "Political orientation (0 L – 10 R)" = feols(polor ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                               | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009 %>% filter(flag_east2)),
  
  "Centre left party ID" = feols(centre_left2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                 | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_east2)),
  
  "Centre right party ID" = feols(centre_right2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) |
                                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_east2))
)


texreg(fe_main_polint_polor2_subset_ee)


texreg(fe_main_polint_polor2_subset_ee, digits = 3,
       label = "tab:fe_main_polint_polor2_subset_ee",
       caption = "Opportunity moves and political integration and orientation (east only)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                             
       custom.note = paste("%stars. All models are restricted to stayers and movers within east Germany and include age group, education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2009/10-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D19.tex"))


#--------------------------------------------------------------------------------------------#

#-------------------------------------------------------------------------------# 
## Party identification ##
#-------------------------------------------------------------------------------# 


# - alternative coding where all those with 0 party identification are in the reference category of the dummies

fe_main_partid2_subset_ee <-  list(
  "SPD " = feols(spd_co2 ~ opportunity_pc01_wn  + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_east2)),
  
  "CDU/CSU " = feols(cducsu_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                       pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_east2)),
  
  "Gruene " = feols(greens_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                      pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_east2)),
  
  "FDP " = feols(fdp_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_east2)),
  
  "Linke" = feols(linke_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(flag_east2)),
  
  "AfD " = feols(afd_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2014 %>% filter(flag_east2))
)

texreg(fe_main_partid2_subset_ee)


texreg(fe_main_partid2_subset_ee, digits = 3,
       label = "tab:fe_main_partid2_subset_ee",
       caption = "Opportunity moves and party identification (east only)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                            
       custom.note = paste("%stars. Party leanings include coalitions in the political direction of the respective party (i.e., including left coalitions for SPD, Gruene, and Linke; right coalitions for CDU/CSU, FDP, and AfD). All models are restricted to stayers and movers within east Germany and include age group, education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2010-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D20.tex"))


#-------------------------------------------------------------------------------# 

## Destination West ##

# Use sample without multiple movers 

# Flag moving destinations

df_2009 <- df_2009 %>%
  group_by(pid) %>%
  mutate(destination_west = ifelse(any(moved_krs2 == 1 & west == 1), 1, 0),
         destination_east = ifelse(any(moved_krs2 == 1 & west == 0), 1, 0)
         ) %>%
  ungroup()


df <- df %>%
  group_by(pid) %>%
  mutate(destination_west = ifelse(any(moved_krs2 == 1 & west == 1), 1, 0),
         destination_east = ifelse(any(moved_krs2 == 1 & west == 0), 1, 0)
  ) %>%
  ungroup()


df_2014 <- df_2014 %>%
  group_by(pid) %>%
  mutate(destination_west = ifelse(any(moved_krs2 == 1 & west == 1), 1, 0),
         destination_east = ifelse(any(moved_krs2 == 1 & west == 0), 1, 0)
  ) %>%
  ungroup()





#-------------------------------------------------------------------------------# 

## Destination West (!= destination_east == 1; exclude those with destination east; control still includes all)


#-------------------------------------------------------------------------------# 
## Political integration and orientation ##
#-------------------------------------------------------------------------------# 

fe_main_polint_polor2_subset_dest_west <-  list(
  "Volunteering (Yes/No)" = feols(ft_volunt_d ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                  | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_east != 1)),
  
  "Local political activism (Yes/No)" = feols(ft_localpol_d ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                              | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_east != 1)),
  
  "Voting in federal election (Yes/No)" = feols(votefedel_lead ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                                | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009 %>% filter(destination_east != 1)),
  
  "Party leaning (Yes/No)" = feols(partyleand2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                   | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_east != 1)),
  
  "Party leaning intensity (0-5)" = feols(partyleanint ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                          | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_east != 1)),
  
  "Political orientation (0 L – 10 R)" = feols(polor ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                               | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009 %>% filter(destination_east != 1)),
  
  "Centre left party ID" = feols(centre_left2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                 | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_east != 1)),
  
  "Centre right party ID" = feols(centre_right2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) |
                                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_east != 1))
)


texreg(fe_main_polint_polor2_subset_dest_west)


texreg(fe_main_polint_polor2_subset_dest_west, digits = 3,
       label = "tab:fe_main_polint_polor2_subset_dest_west",
       caption = "Opportunity moves and political integration and orientation (destination west only)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                             
       custom.note = paste("%stars. All models restrict treated individuals to those with west Germany as relocation destination and include age group, education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2009/10-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D21.tex"))


#--------------------------------------------------------------------------------------------#

#-------------------------------------------------------------------------------# 
## Party identification ##
#-------------------------------------------------------------------------------# 


# - alternative coding where all those with 0 party identification are in the reference category of the dummies

fe_main_partid2_subset_dest_west <-  list(
  "SPD " = feols(spd_co2 ~ opportunity_pc01_wn  + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_east != 1)),
  
  "CDU/CSU " = feols(cducsu_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                       pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_east != 1)),
  
  "Gruene " = feols(greens_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                      pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_east != 1)),
  
  "FDP " = feols(fdp_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_east != 1)),
  
  "Linke" = feols(linke_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_east != 1)),
  
  "AfD " = feols(afd_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2014 %>% filter(destination_east != 1))
)

texreg(fe_main_partid2_subset_dest_west)


texreg(fe_main_partid2_subset_dest_west, digits = 3,
       label = "tab:fe_main_partid2_subset_dest_west",
       caption = "Opportunity moves and party identification (destination west only)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                            
       custom.note = paste("%stars. Party leanings include coalitions in the political direction of the respective party (i.e., including left coalitions for SPD, Gruene, and Linke; right coalitions for CDU/CSU, FDP, and AfD). All models restrict treated individuals to those with west Germany as relocation destination and include age group, education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2010-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D22.tex"))



#-------------------------------------------------------------------------------# 

## Destination East (!= destination_west == 1; exclude those with destination west; control still includes all)


#-------------------------------------------------------------------------------# 
## Political integration and orientation ##
#-------------------------------------------------------------------------------# 

fe_main_polint_polor2_subset_dest_east <-  list(
  "Volunteering (Yes/No)" = feols(ft_volunt_d ~ opportunity_pc01_wn + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                  | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_west != 1)),
  
  "Local political activism (Yes/No)" = feols(ft_localpol_d ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                              | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_west != 1)),
  
  "Voting in federal election (Yes/No)" = feols(votefedel_lead ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                                | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009 %>% filter(destination_west != 1)),
  
  "Party leaning (Yes/No)" = feols(partyleand2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                   | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_west != 1)),
  
  "Party leaning intensity (0-5)" = feols(partyleanint ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype)
                                          | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_west != 1)),
  
  "Political orientation (0 L – 10 R)" = feols(polor ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                               | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2009 %>% filter(destination_west != 1)),
  
  "Centre left party ID" = feols(centre_left2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) 
                                 | pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_west != 1)),
  
  "Centre right party ID" = feols(centre_right2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) |
                                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_west != 1))
)


texreg(fe_main_polint_polor2_subset_dest_east)


texreg(fe_main_polint_polor2_subset_dest_east, digits = 3,
       label = "tab:fe_main_polint_polor2_subset_dest_east",
       caption = "Opportunity moves and political integration and orientation (destination east only)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                             
       custom.note = paste("%stars. All models restrict treated individuals to those with east Germany as relocation destination and include age group, education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2009/10-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D23.tex"))


#--------------------------------------------------------------------------------------------#

#-------------------------------------------------------------------------------# 
## Party identification ##
#-------------------------------------------------------------------------------# 


# - alternative coding where all those with 0 party identification are in the reference category of the dummies

fe_main_partid2_subset_dest_east <-  list(
  "SPD " = feols(spd_co2 ~ opportunity_pc01_wn  + as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_west != 1)),
  
  "CDU/CSU " = feols(cducsu_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                       pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_west != 1)),
  
  "Gruene " = feols(greens_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                      pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_west != 1)),
  
  "FDP " = feols(fdp_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_west != 1)),
  
  "Linke" = feols(linke_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                    pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df %>% filter(destination_west != 1)),
  
  "AfD " = feols(afd_co2 ~ opportunity_pc01_wn +  as.factor(age_grp3) + as.factor(educ_h) + as.factor(hhtype) | 
                   pid + syear, panel.id = ~pid + syear, cluster = "krs", data = df_2014 %>% filter(destination_west != 1))
)

texreg(fe_main_partid2_subset_dest_east)


texreg(fe_main_partid2_subset_dest_east, digits = 3,
       label = "tab:fe_main_partid2_subset_dest_east",
       caption = "Opportunity moves and party identification (destination east only)",
       custom.coef.map = list('opportunity_pc01_wn' = 'Opportunity Index'),
       custom.gof.rows = list("Individual fixed-effects" = c("YES", "YES", "YES", "YES", "YES", "YES"), "Time fixed-effect" = c("YES", "YES", "YES", "YES", "YES", "YES")),
       stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.adjrs = F,
       custom.gof.names = vec_gof_names,                                            
       custom.note = paste("%stars. Party leanings include coalitions in the political direction of the respective party (i.e., including left coalitions for SPD, Gruene, and Linke; right coalitions for CDU/CSU, FDP, and AfD). All models restrict treated individuals to those with east Germany as relocation destination and include age group, education group, and household type as control variables; Standard errors are clustered at the Kreis-level. Source: SOEP v.37, 2010-2020."),
       file = paste0(tables_path, "robustness_heterogeneity/Appendix_Table_D24.tex"))





#-------------------------------------------------------------------------------# 

# Remove all model lists from storage

rm(list = ls(pattern = "^fe_"))
#-------------------------------------------------------------------------------# 
#-------------------------------------------------------------------------------# 

